
package net.atremmedia.gwt.actionjava.examples.sgi;

import actionjava.anim.Anim;
import actionjava.anim.ITween;
import actionjava.anim.config.Manifest;
import actionjava.anim.ease.ExpoOut;
import actionjava.display.Graphics;
import actionjava.display.Shape;
import actionjava.display.Sprite;
import actionjava.display.TextField;
import actionjava.events.mouse.MouseEvent;
import actionjava.events.mouse.MouseEvent.MouseEventType;
import actionjava.events.mouse.MouseOutHandler;
import actionjava.events.mouse.MouseOverHandler;
import actionjava.events.mouse.MouseUpHandler;
import actionjava.geom.Color;
import com.google.gwt.dom.client.Style.Cursor;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;

public class HighlightButton
{
	private HighlightLinkVO hlVO;
	private TextField titleField;
	private Shape background;
	private SGI2RegHeader stage;
	private double x;
	private double y;
	private String color;
	private Sprite arrow;
	private ITween tween1 = null;
	private ITween tween2 = null;

	public HighlightButton(double x, double y, String color, SGI2RegHeader stage, HighlightLinkVO hlVO)
	{
		this.x = x;
		this.y = y;
		this.color = color;
		this.stage = stage;
		this.hlVO = hlVO;
		this.initialize();
	}

	private void initialize()
	{
		createTitleField();
		createBG();
		createArrow();
		setEvents();
	}

	private void setEvents()
	{
		background.addEventListener(MouseEventType.MOUSE_OVER, new MouseOverHandler()
		{
			@Override
			protected void mouseOverHandler(MouseEvent event)
			{
				RootPanel.get().getElement().getStyle().setCursor(Cursor.POINTER);
				titleField.setColor(color);
				arrowIn();
			}
		});

		background.addEventListener(MouseEventType.MOUSE_OUT, new MouseOutHandler()
		{
			@Override
			protected void mouseOutHandler(MouseEvent event)
			{
				RootPanel.get().getElement().getStyle().setCursor(Cursor.DEFAULT);
				titleField.setColor("#000000");
				arrowOut();
			}
		});

		background.addEventListener(MouseEventType.MOUSE_UP, new MouseUpHandler()
		{
			@Override
			protected void mouseUpHandler(MouseEvent event)
			{
				Window.open(hlVO.getUrl(), "_self", "");
			}
		});
	}

	private void createBG()
	{
		background = new Shape();
		background.setX(x);
		background.setY(y);

		Graphics graphs = background.getGraphics();
		graphs.beginFill(new Color(255, 255, 255).getHexValue());
		graphs.drawRect(0, 0, 200, 15);
		graphs.endFill();
		stage.addChild(background);
	}

	private void createTitleField()
	{
		titleField = new TextField(hlVO.getTitle(), "Arial", "#000000");
		titleField.setSize(15);
		titleField.setX(x);
		titleField.setY(y);
		stage.addChild(titleField);
	}

	private void createArrow()
	{
		arrow = new Sprite();
		arrow.setAlpha(0);
		arrow.setX(x - 15);
		arrow.setY(y + 8);

		Shape arrowMask = new Shape();
		Graphics graph = arrowMask.getGraphics();
		graph.beginStroke("#000");
		graph.beginFill(Color.getRGBA(0xFF0000, 1));
		try {
			graph.decodePath("AgngxIBPAxIhPAxg");
		} catch (Exception e) {
			e.printStackTrace();
		}
		graph.closePath();

		Shape arrowColor = new Shape();
		graph = arrowColor.getGraphics();
		graph.beginFill(color);
		graph.drawRect(-4, -5, 8, 10);
		arrowColor.setMask(arrowMask);
		arrow.addChild(arrowColor);

		stage.addChild(arrow);
	}

	private void arrowIn()
	{
		if(tween2 != null) {
			tween2.pause();
		}

		tween1 = Anim.tween(new Manifest(arrow, .5, ExpoOut.ease)
		{
			@Override
			protected void configure()
			{
				to(X, (x - 8));
				to(ALPHA, 1);
			}
		});
	}

	private void arrowOut()
	{
		if(tween1 != null) {
			tween1.pause();
		}

		tween2 = Anim.tween(new Manifest(arrow, .5, ExpoOut.ease)
		{
			@Override
			protected void configure()
			{
				to(X, (x - 15));
				to(ALPHA, 0);
			}
		});
	}
}
